package org.suze.redis.protocol;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @description:
 * @Date : 2018/9/21 下午8:57
 * @Author : 石冬冬-Seig Heil(dongdong.shi@mljr.com)
 */
public class Protocol {
    public static final String DOLLAR_STRING = "$";
    public static final String ASTERISK_STRING = "*";
    public static final String BLANK_STRING = "\r\n";

    /**
     * 发送命令
     * @param os
     * @param command
     */
    public static void sendCommand(OutputStream os,Command command,byte[]...args){
        StringBuffer sb = new StringBuffer();
        sb.append(ASTERISK_STRING).append(args.length + 1).append(BLANK_STRING);
        sb.append(DOLLAR_STRING).append(args.length + 1).append(BLANK_STRING);
        sb.append(command.name()).append(BLANK_STRING);
        for (final byte[] arg : args){
            sb.append(DOLLAR_STRING).append(arg.length).append(BLANK_STRING);
            sb.append(new String(arg)).append(BLANK_STRING);
        }
        try {
            os.write(sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static enum Command{
        SET,GET,KEYS
    }
}
